package com.jinzhi.jzapi.certificate.controller;

import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.configconsts.wxconsts.LinkConfiggure;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.*;
import com.jinzhi.common.vo.WxPayConfig;
import com.jinzhi.common.vo.WxPayVo;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserRoleService;
import com.jinzhi.sys.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.impl.ChainBase;
import org.apache.commons.chain.impl.ContextBase;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

import static com.jinzhi.api.util.TokenTag.TOKEN_TAG;

@Slf4j
@RestController
@RequestMapping("/jzApi/certificates")
public class ApiCertificatesController extends BaseController {

    @Autowired
    private CertificatesService certificatesService;
    @Autowired
    private UserService userService;
    @Autowired
    private CertificateNewsService certificateNewsService;
    @Autowired
    private CertificationBatchService certificationBatchService;
    @Autowired
    private StuSignUpService stuSignUpService;
    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private OrganizationNewsService organizationNewsService;
    @Autowired
    private KnowledgePointsService knowledgePointsService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private CertificateProfileService certificateProfileService;
    @Autowired
    private WxPayConfig wxPayConfig;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ChainBase wxPaySignUpChain, wxPayNotifySignUpChain;
    @Autowired
    private MsgTemplateService msgTemplateService;

    private static BigDecimal bigDecimal;

    static {
        bigDecimal = new BigDecimal(100);
    }

    /**
     * api小程序端-证书查询--公开方法，不需要登录
     *
     * @Description: 根据证书编号查询证书信息，参数：证书编号
     * @Param: [certificateNo]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-03
     */
    @GetMapping("/selCertificate")
    @Log("api小程序端-证书查询")
    @ApiOperation(value = "api小程序端-证书查询", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ApiResult<?> selCertificate(String certificateNo) {
        //第一步，根据证书编号查询证书
        CertificatesDO certificatesDO = certificatesService.selAll(certificateNo);
        if (certificatesDO != null) {
            Map map = new HashMap();
            map.put("cerImg",certificatesDO.getImgUrl());//证书图片
            map.put("certificateNo", certificatesDO.getNumbers());//证书编号
            map.put("certificateName", certificatesDO.getCertificateName());//证书名称
            map.put("addTime", certificatesDO.getGetTime());//获证时间
            map.put("isEffective", certificatesDO.getState());//证书是否有效,0有效，1无效

            //第二步，根据证书中的证书信息id，查询证书基本信息
            CertificateNewsDO certificateNewsDO = certificateNewsService.selectById(certificatesDO.getNewsId());
            if (certificateNewsDO != null) {
                map.put("grade", certificateNewsDO.getGrade());//证书等级
            } else {
                return ApiResult.fail("该编号的证书信息错误");
            }

            //第三步，根据证书中的用户id，查询用户信息
            UserDO user = userService.selectById(certificatesDO.getUserId());
            if (user != null) {
                map.put("name", user.getName());//获证人姓名
                map.put("headPortrait", user.getHeadPortrait());//获证人头像
            } else {
                return ApiResult.fail("该编号的证书信息错误");
            }

            //第四步，添加查询次数
            int queries = certificatesDO.getQueries() + 1;
            certificatesDO.setQueries(queries);
            certificatesService.updateById(certificatesDO);

            //第五步，封装返回map，并返回数据
            return ApiResult.success(map);
        } else {
            return ApiResult.fail("该编号的证书信息不存在");
        }
    }


    /**
     * api小程序端-认证批次列表数据--公开方法，不需要登录
     *
     * @Description: 查询认证批次信息
     * @Param: []
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-03
     */
    @GetMapping("/selCertificateList")
    @Log("api小程序端-认证批次列表数据")
    @ApiOperation(value = "api小程序端-认证批次列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ApiResult<?> selCertificateList() {
        //sql关联查询在报名时间内的批次信息并直接封装成map返回
        List<Map<Object, Object>> map = certificationBatchService.selTimeAll();
        return ApiResult.success(map);
    }

    /**
     * api小程序端-考试批次列表数据--公开方法，不需要登录
     *
     * @Description: 查询认考试批次信息
     * @Param: []
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-03
     */
    @GetMapping("/selBatchComList")
    @Log("api小程序端-考试批次列表数据")
    @ApiOperation(value = "api小程序端-考试批次列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ApiResult<?> selBatchComList() {
        //sql关联查询在报名时间内的批次信息并直接封装成map返回
        List<Map<Object, Object>> map = examinationBatchService.selTimeComAll();
        return ApiResult.success(map);
    }


    /**
     * api小程序端-考试批次列表数据--公开方法，不需要登录
     *
     * @Description: 查询认考试批次信息
     * @Param: []
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-03
     */
    @GetMapping("/selBatchList")
    @Log("api小程序端-考试批次列表数据")
    @ApiOperation(value = "api小程序端-考试批次列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ApiResult<?> selBatchList() {
        //sql关联查询在报名时间内的批次信息并直接封装成map返回
        List<Map<Object, Object>> map = examinationBatchService.selTimeAll();
        return ApiResult.success(map);
    }


    /**
     * api小程序端-认证批次证书详情--公开方法，不需要登录
     *
     * @Description: 根据认证批次id查询认证批次详情信息，参数：认证批次id
     * @Param: [id]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-03
     */
    @GetMapping("/selCertificateDetail")
    @Log("api小程序端-认证批次证书详情")
    @ApiOperation(value = "api小程序端-认证批次证书详情", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ApiResult<?> selCertificateDetail(Long id) {
        //sql关联查询在报名时间内的批次信息并直接封装成map返回
        Map map = certificationBatchService.selDetail(id);
        return ApiResult.success(map);
    }

    /**
     * api小程序端-获取认证批次相关机构信息--公开方法，不需要登录
     *
     * @Description: 根据认证批次id查询所有负责的机构信息，参数：认证批次id
     * @Param: [cbatchId]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-23
     */
    @GetMapping("/selCerOrgS")
    @Log("api小程序端-获取认证批次相关机构信息")
    @ApiOperation(value = "api小程序端-获取认证批次相关机构信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ApiResult<?> selCerOrgS(String cbatchId,String name) {
        CertificationBatchDO certificationBatchDO = certificationBatchService.selectById(Long.parseLong(cbatchId));
        List<Map<Object, Object>> maps = organizationNewsService.selCerOrg(certificationBatchDO.getProfileId().toString(),name);
        return ApiResult.success(maps);
    }

    /**
     * api小程序端-证书考试填报信息
     *
     * @Description: 考生报名信息提交，参数：token令牌，考生照片，认证批次id，考生姓名，身份证，报考机构id
     * @Param: [token, photo, chatchId, name, idNumber，orgId，sex]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-03
     */
    @PostMapping("/addCertificateFill")
    @Log("api小程序端-证书考试填报信息")
    @ApiOperation(value = "api小程序端-证书考试填报信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult<?> addCertificateFill(@RequestHeader(value = TOKEN_TAG, required = false) String token,
                                           String photo, String cbatchId, String name, String idNumber, String orgId , Integer type) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }
        MsgTemplateDO msg = msgTemplateService.findOneByKv("type" , 0, "temp_ip" , 101);
        /*if (msg != null){//审核通知模板id
            map.put("tempId" , msg.getCode());
        }*/

        //2.根据认证批次和会员id进行查询，如果该批次考试该会员报名后，不能再次报名
        Map map = new HashMap();
        map.put("userId", user.getId());
        map.put("chatchId", cbatchId);
        Map map1 = stuSignUpService.selCateFill(map);
        if (map1 == null || map1.size() == 0) {
            Long sex = null;
            //判断身份证中性别信息和导入性别是否一致
            String sexNum = idNumber.substring(idNumber.length() - 2, idNumber.length() - 1);
            int jj = Integer.parseInt(sexNum) & 1;

            if (jj == 1) {
                sex = 1L;//男
            }
            if (jj == 0) {
                sex = 2L;//女
            }

            //3.新建报名信息
            StuSignUpDO stuSignUpDO = new StuSignUpDO();
            stuSignUpDO.setUserId(user.getId());//会员id
            stuSignUpDO.setCbatchId(Long.parseLong(cbatchId));//认证批次id
            stuSignUpDO.setIdNumber(idNumber);//身份证号码
            stuSignUpDO.setName(name);//考生姓名
            stuSignUpDO.setPhoto(photo);//考生照片
            stuSignUpDO.setType(type);
            stuSignUpDO.setSignUpTime(new Date());//报名时间
            stuSignUpDO.setState(0);//审核状态，默认未审核
            stuSignUpDO.setOrgId(Long.parseLong(orgId));//考生选择考点
            boolean b = stuSignUpService.insert(stuSignUpDO);
            if (b) {
                boolean j = false;
                if (StringUtils.isBlank(user.getHeadPortrait())) {
                    user.setHeadPortrait(photo);//证件照
                    j = true;
                }
                if (StringUtils.isBlank(user.getIdNumber())) {
                    user.setIdNumber(idNumber);//身份证号码
                    j = true;
                }
                if (StringUtils.isBlank(user.getName())) {
                    user.setName(name);//同时更新到用户表真实姓名中
                    j = true;
                }
                if (user.getSex() == null || user.getSex() == 0) {
                    user.setSex(sex);
                    j = true;
                }else if(!user.getSex().equals(sex)){
                    user.setSex(sex);
                    j = true;
                }

                if (j) {
                    //4.判断用户角色信息
                    List<Long> roleIds = userRoleService.listRoleId(user.getId());
                    if (roleIds != null && roleIds.size() > 0) {
                        user.setroleIds(roleIds);
                    } else {
                        List<Long> list = new ArrayList<>();
                        list.add(56L);
                        user.setroleIds(list);
                    }
                    boolean c = userService.updateById(user);
                    if (c) {
                        return ApiResult.success(msg.getCode());
                    } else {
                        return ApiResult.fail();
                    }
                } else {
                    return ApiResult.success(msg.getCode());
                }

            } else {
                return ApiResult.fail();
            }
        } else {
            //3.提示已报名
            return ApiResult.build(ApiEnumError.EnrolmentError.getCode(), ApiEnumError.EnrolmentError.getMsg());
        }
    }

    /**
     * api小程序端-考生报名信息修改
     *
     * @Description: 考生报名信息修改，参数：token令牌，考生照片，考生报名信息id，考生姓名，身份证，报考机构id
     * @Param: [token, photo, stuSignUpId, name, idNumber，orgId]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-03
     */
    @PostMapping("/editCertificateFill")
    @Log("api小程序端-考生报名信息修改")
    @ApiOperation(value = "api小程序端-考生报名信息修改", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult<?> editCertificateFill(@RequestHeader(value = TOKEN_TAG, required = false) String token, String stuSignUpId,
                                            String photo, String name, String idNumber, String orgId,String cbatchId) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }
        Map map = new HashMap();
        MsgTemplateDO msg = msgTemplateService.findOneByKv("type" , 0 , "temp_ip" , 101);
        if (msg != null){
            map.put("tempId" , msg.getCode());
        }else {
            map.put("tempId" , null);
        }
        CertificationBatchDO certificationBatchDO = certificationBatchService.selectById(cbatchId);
        if(certificationBatchDO != null){
           boolean ccr = DateUtil.isEffectiveDate(new Date(),certificationBatchDO.getStartTime(),certificationBatchDO.getEndTime());
           if(!ccr){
               return ApiResult.build(ApiEnumError.RegistrationIsClosed.getCode(), ApiEnumError.RegistrationIsClosed.getMsg());
           }
        }

        Long sex = null;
        //判断身份证中性别信息和导入性别是否一致
        String sexNum = idNumber.substring(idNumber.length() - 2, idNumber.length() - 1);
        int jj = Integer.parseInt(sexNum) & 1;

        if (jj == 1) {
            sex = 1L;//男
        }
        if (jj == 0) {
            sex = 2L;//女
        }

        StuSignUpDO stuSignUpDO = stuSignUpService.selectById(Long.parseLong(stuSignUpId));
        if (stuSignUpDO != null) {
            stuSignUpDO.setUserId(user.getId());//会员id
            stuSignUpDO.setIdNumber(idNumber);//身份证号码
            stuSignUpDO.setName(name);//考生姓名
            stuSignUpDO.setPhoto(photo);//考生照片
            stuSignUpDO.setState(0);//审核状态，默认未审核
            stuSignUpDO.setOrgId(Long.parseLong(orgId));//考生选择考点
            boolean b = stuSignUpService.updateById(stuSignUpDO);
            if (b) {
                boolean j = false;
                if (!user.getIdNumber().equals(idNumber)) {
                    user.setIdNumber(idNumber);//身份证号码
                    j = true;
                }
                if (!user.getName().equals(name)) {
                    user.setName(name);//同时更新到用户表真实姓名中
                    j = true;
                }
                if(!user.getSex().equals(sex)){
                    user.setSex(sex);
                    j = true;
                }

                if (j) {
                    //4.判断用户角色信息
                    List<Long> roleIds = userRoleService.listRoleId(user.getId());
                    if (roleIds != null && roleIds.size() > 0) {
                        user.setroleIds(roleIds);
                    } else {
                        List<Long> list = new ArrayList<>();
                        list.add(56L);
                        user.setroleIds(list);
                    }
                    boolean c = userService.updateById(user);
                    if (c) {
                        return ApiResult.success(map);
                    } else {
                        return ApiResult.fail();
                    }
                } else {
                    return ApiResult.success(map);
                }
            } else {
                return ApiResult.build(ApiEnumError.EDITERR.getCode(), ApiEnumError.EDITERR.getMsg());
            }
        } else {
            //3.提示没有查到报名信息
            return ApiResult.build(ApiEnumError.NoInfrom.getCode(), ApiEnumError.NoInfrom.getMsg());
        }
    }


    /**
     * api小程序端-考生报名信息查询
     * @Description: 根据认证批次id查询考生报名信息，参数：token令牌，认证批次id
     * @Param: [token, chatchId]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-03
     */
    @GetMapping("/selCertificateFill")
    @Log("api小程序端-考生报名信息查询")
    @ApiOperation(value = "api小程序端-考生报名信息查询", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresAuthentication
    @Transactional
    public ApiResult<?> selCertificateFill(@RequestHeader(value = TOKEN_TAG, required = false) String token, String chatchId) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }
        Map map = new HashMap();
        map.put("userId", user.getId());
        map.put("chatchId", Long.parseLong(chatchId));
        Map map1 = stuSignUpService.selCateFill(map);
        return ApiResult.success(map1);
    }


    /**
     * api小程序端-知识点列表查询接口--公开方法，不需要登录
     *
     * @Description: 查询知识点列表信息
     * @Param: []
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-04
     */
    @GetMapping("/selKpointsFill")
    @Log("api小程序端-知识点列表查询接口")
    @ApiOperation(value = "api小程序端-知识点列表查询接口", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ApiResult<?> selKpointsFill(String profileId) {
       List<Map<Object, Object>> maps = knowledgePointsService.selByAll();
//        List<Map<Object, Object>> maps = knowledgePointsService.findByKv("profile_id" , profileId ,"state" ,0);
        return ApiResult.success(maps);
    }


    /**
     * api小程序端-证书概况信息列表接口--公开方法，不需要登录
     *
     * @Description: 查询证书概况信息列表
     * @Param: []
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-04
     */
    @GetMapping("/selProfileFill")
    @Log("api小程序端-证书概况新列表接口")
    @ApiOperation(value = "api小程序端-证书概况新列表接口", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ApiResult<?> selProfileFill() {
        List<Map<Object, Object>> maps = certificateProfileService.selByFormal();
        return ApiResult.success(maps);
    }


    /**
     * api小程序端-考生报名微信支付接口
     *
     * @Description: 微信支付，参数：token令牌，wxpayVO虚拟实体（接收参数total_fee-支付金额，body-支付内容，trade_type-支付类型），考生报名id
     * @Param: [token, wxPayVo, stuSignUpId]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-03-12
     */
    @PostMapping("/wxPay")
    @Log("api小程序端-考生报名微信支付接口")
    @ApiOperation(value = "api小程序端-考生报名微信支付接口", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresAuthentication
    @Transactional
    public ApiResult<?> wxPay(@RequestHeader(value = TOKEN_TAG, required = false) String token, @Validated WxPayVo wxPayVo, String stuSignUpId) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        ApiResult apiResult = new ApiResult();
        try {
            //判断考生报名id是否为空
            if (null == stuSignUpId || stuSignUpId == "undefined") {
                return ApiResult.build(ApiEnumError.lackParameter.getCode(), "缺少参数:stuSignUpId");
            }

            if(StringUtils.isBlank(user.getCerOpenId())){
                return ApiResult.build(ApiEnumError.NOOPENID.getCode(), ApiEnumError.NOOPENID.getMsg());
            }

            String cerName = "考生线上报名";
            Double amouts = -1.0;
            StuSignUpDO stuSignUpDO = stuSignUpService.selectById(Long.parseLong(stuSignUpId));
            if(stuSignUpDO == null){
                return ApiResult.build(ApiEnumError.dateGetError.getCode(),ApiEnumError.dateGetError.getMsg());
            }else{
                CertificationBatchDO certificationBatchDO = certificationBatchService.selectById(stuSignUpDO.getCbatchId());
                if(certificationBatchDO == null){
                    return ApiResult.build(ApiEnumError.dateGetError.getCode(),ApiEnumError.dateGetError.getMsg());
                }else{
                    cerName = certificationBatchDO.getName() + "--线上报名";
                    CertificateNewsDO certificateNewsDO = certificateNewsService.selectById(certificationBatchDO.getNewsId());
                    if(certificateNewsDO == null){
                        return ApiResult.build(ApiEnumError.dateGetError.getCode(),ApiEnumError.dateGetError.getMsg());
                    }else{
                        amouts = certificateNewsDO.getExamFee();
                    }
                }
            }

            if(amouts == -1.0){
                return ApiResult.build(ApiEnumError.dateGetError.getCode(),ApiEnumError.dateGetError.getMsg());
            }

            OrderDO orderDOs = orderService.findOneByKv("object_id",Long.parseLong(stuSignUpId));
            if(orderDOs == null){
                //新增订单信息 --2020-05-18 kl
                OrderDO orderDO = new OrderDO();
                orderDO.setType(1);//订单类型为报名
                orderDO.setUserId(user.getId());//用户id
                orderDO.setObjectId(Long.parseLong(stuSignUpId));//考生id
                orderDO.setOrderAmount(amouts);//订单金额
                orderDO.setName(cerName);//订单名称
                orderDO.setState(1);//默认未付款
                orderDO.setOpenId(user.getCerOpenId());//存入openId
                boolean bbc = orderService.insert(orderDO);
                if(!bbc){
                    return ApiResult.build(ApiEnumError.ORDERSAVEERR.getCode(),ApiEnumError.ORDERSAVEERR.getMsg());
                }
            }

            String openId = user.getCerOpenId();
            //通过uuid生成商户id
            String out_trade_no = UUID.randomUUID().toString().replace("-", "");
            //生成32为随机数
            String randomStringBy = StringUtil.getRandomStringByLength(32);

            wxPayVo.setAppid(wxPayConfig.getAppId());//appid（微信公众号）
            wxPayVo.setMch_id(wxPayConfig.getMerNo());//商户id
            wxPayVo.setNotify_url(wxPayConfig.getSignUpNotify());//回调地址
            wxPayVo.setSpbill_create_ip(wxPayConfig.getIp());//ip地址
            wxPayVo.setOut_trade_no(out_trade_no);
            wxPayVo.setNonce_str(randomStringBy);
            wxPayVo.setTotal_fee(new BigDecimal(amouts.toString()).multiply(bigDecimal).setScale(0).toString());//支付金额
            wxPayVo.setOpenId(openId);
            wxPayVo.setLabel(WxPayLabelEnum.stupOrder.getTag());
            wxPayVo.setType(AppEnum.certificates.getTag());

            log.info("商户订单号:" + out_trade_no);

            //保存用户id至缓存，为后面回调提供
            EhCacheUtil.put(out_trade_no + ":stuSignUpId", Long.parseLong(stuSignUpId));

            //创建上下文
            Context context = new ContextBase();

            //微信支付对象
            context.put(LinkConfiggure.WX_PAY_VO, wxPayVo);
            //固定返回对象
            context.put(LinkConfiggure.API_RESULT, apiResult);
            //订单id(考生报名id）
            context.put("stuSignUpId", Long.parseLong(stuSignUpId));
            //用户id
            context.put("user_id", user.getId());
            //商户订单号
            context.put("out_trade_no", out_trade_no);

            //开始链路
            wxPaySignUpChain.execute(context);
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new RuntimeException(e);
        }
        Map map = new HashMap();
        map.put("type", "wx");
        map.put("wx", apiResult);
        return ApiResult.success(map);
    }


    /**
     * 微信支付回调接口
     *
     * @Description: 微信支付回调，供微信调用
     * @Param: [request]
     * @return: java.lang.String
     * @Author: kl
     * @Date: 2020-03-12
     */
    @ExceptionHandler(Exception.class)
    @PostMapping("/wxNotify")
    public String wxNotify(HttpServletRequest request) {
        StringUtil stringUtil = new StringUtil();
        return stringUtil.getWxNotify(request, wxPayNotifySignUpChain, wxPayConfig);
    }

    @GetMapping("/selCertificates")
    @Log("认证批次列表数据")
    @ApiOperation(value = "api小程序端-认证批次列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ApiResult<?> selCertificates() {
        List<Map<Object, Object>> map = certificationBatchService.selByNameId();
        return ApiResult.success(map);
    }
}
